package com.springmvc.controller;

import com.springmvc.entity.User;
import com.springmvc.service.UserService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class ViewController {

    private Logger logger = Logger.getLogger(ViewController.class);

    @Resource(name = "UserService")
    private UserService userService;


    @RequestMapping("/add")
    public String index() {
        return "add";
    }


    @RequestMapping("/find")
    @ResponseBody
    public Map<String, Object> find(User user, HttpServletRequest request) {

        Map<String, Object> map = new HashedMap();
        System.out.println("你已通过springMVC进入controller方法。。。。");
        logger.info("你已通过springMVC进入controller方法。。。。");
        User loginuser = userService.findByUsernameAndPwd(user.getUsername(), user.getPassword());
        if (loginuser != null) {
            map.put("result", "success");
        } else {
            map.put("result", "fail");
        }
        return map;
    }


    @RequestMapping("/findUser")
    @ResponseBody
    public Map<String, Object> findUser(User user, HttpServletRequest request) {
        Map<String, Object> map = new HashedMap();
        List<User> uList = userService.findUserInfo(user.getUsername());
        if (uList != null) {
            map.put("result", uList);
        } else {
            map.put("result", uList);
        }
        return map;

    }

    @RequestMapping("/success")
    public String success() {
        System.out.println("登录成功。。。。");
        logger.info("登录成功。。。。");

        return "success";
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public Map<String, Object> addUser(HttpServletRequest request) {
        String result = "error";
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");

        User u = new User();
        u.setId(id);
        u.setUsername(username);
        u.setPassword(password);
        u.setSex(sex);

        int flag = userService.add(u);
        if (flag > 0) {
            System.out.println("用户添加成功。。。。");
            logger.info("用户添加成功。。。。");
            result = "successful";
        } else {
            result = "error";
        }
        Map<String, Object> mapR = new HashedMap();
        mapR.put("result", result);
        return mapR;
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest req) {
        String result = "fail";
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");


        int flag = userService.delete(id);
        if (flag > 0) {
            result = "success";
        } else {
            result = "fail";
        }

        Map<String, Object> map = new HashedMap();
        map.put("result", result);
        return map;
    }


    @RequestMapping("/updateUser")
    @ResponseBody
    public Map<String, Object> updateUser(HttpServletRequest req) {
        String result = "fail";
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");


        User u = new User();
        u.setId(id);
        u.setUsername(username);
        u.setPassword(password);
        u.setSex(sex);

        int flag = userService.update(u);
        if (flag > 0) {
            result = "success";
        } else {
            result = "fail";
        }

        Map<String, Object> map = new HashedMap();
        map.put("result", result);
        return map;
    }


    @RequestMapping("/addSuccess")
    public String addSuccess() {
        System.out.println("用户添加成功。。。。");
        logger.info("用户添加成功。。。。");
        return "addSuccess";
    }

}
